<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
                <a-input v-model="model.orderNo" placeholder="请输入订单编号" disabled></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskName">
                <a-input v-model="model.taskName" placeholder="请输入任务名称" ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="商务人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessPerson">
                <j-select-user-by-dep v-model="model.businessPerson" />
              </a-form-model-item>
            </a-col>
          </a-row>
<!--          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerName">
                <a-input v-model="model.customerName" placeholder="请输入客户名称" ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="客户电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerPhone">
                <a-input v-model="model.customerPhone" placeholder="请输入客户电话" ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="客户税号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerTaxId">
                <a-input v-model="model.customerTaxId" placeholder="请输入客户税号" ></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>-->
          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="模型数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelCount">
                <a-input-number v-model="model.modelCount" placeholder="请输入模型数量" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="模型克重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelWeight">
                <a-input-number v-model="model.modelWeight" placeholder="请输入模型克重" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="上机克重" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineWeight">
                <a-input-number v-model="model.machineWeight" placeholder="请输入上机克重" style="width: 100%" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="下单时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderTime">
                <j-date placeholder="请选择下单时间" v-model="model.orderTime" style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="交付时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="requiredDeliveryTime">
                <j-date placeholder="请选择交付时间" v-model="model.requiredDeliveryTime" style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="总工期/天" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalDays">
                <a-input v-model="model.totalDays" placeholder="请输入总工期/天" ></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
<!--          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="单克金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quotePerGram">
                <a-input v-model="model.quotePerGram" placeholder="请输入单克金额" ></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="预估金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="estimatedAmount">
                <a-input-number v-model="model.estimatedAmount" placeholder="请输入预估金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="实际金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualAmount">
                <a-input-number v-model="model.actualAmount" placeholder="请输入实际金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
          </a-row>-->
<!--          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="设计金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="designAmount">
                <a-input-number v-model="model.designAmount" placeholder="请输入设计金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="已收金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receivedAmount">
                <a-input-number v-model="model.receivedAmount" placeholder="请输入已收金额" style="width: 100%" />
              </a-form-model-item>
            </a-col>
          </a-row>-->
          <a-row>
            <a-col :span="12" >
              <a-form-model-item label="单子类型" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="orderType">
                <j-dict-select-tag type="radio" v-model="model.orderType" dictCode="order_type" placeholder="请选择单子类型" />
              </a-form-model-item>
            </a-col>
<!--            <a-col :span="12" >
              <a-form-model-item label="货款状态" :labelCol="labelCol3" :wrapperCol="wrapperCol3" prop="paymentStatus">
                <j-dict-select-tag type="radio" v-model="model.paymentStatus" dictCode="payment_status" placeholder="请选择货款状态" />
              </a-form-model-item>
            </a-col>-->
          </a-row>
          <a-row>
          </a-row>
          <a-row>
            <a-col :span="24" >
              <a-form-model-item label="发货方式" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="deliveryMethod">
                <j-dict-select-tag type="radio" v-model="model.deliveryMethod" dictCode="delivery_method" placeholder="请选择发货方式" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24" >
              <a-form-model-item   label="客户要求" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="customerRequirements">
                <a-textarea v-model="model.customerRequirements" rows="5" placeholder="请输入客户要求" />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="8" >
              <a-form-model-item label="订单图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderImages">
                <j-image-upload isMultiple  v-model="model.orderImages" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
              <a-form-model-item label="订单模型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderModel">
                <j-upload v-model="model.orderModel"  ></j-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="8" >
            </a-col>
          </a-row>
          <a-col :span="24">
            <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskName">
              <a-input v-model="model.taskName" placeholder="请输入任务名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入订单编号"  ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="订单ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderId">
              <a-input v-model="model.orderId" placeholder="请输入订单ID"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="设计工期开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plannedStartTime">
              <j-date placeholder="请选择设计工期开始时间" v-model="model.plannedStartTime"  style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计工期结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plannedEndTime">
              <j-date placeholder="请选择设计工期结束时间" v-model="model.plannedEndTime"  style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计实际开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualStartTime">
              <j-date placeholder="请选择设计实际开始时间" v-model="model.actualStartTime"  style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计实际交付时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualEndTime">
              <j-date placeholder="请选择设计实际交付时间" v-model="model.actualEndTime"  style="width: 100%" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计实际总工时(小时)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actualHours">
              <a-input-number v-model="model.actualHours" placeholder="请输入设计实际总工时(小时)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="designRemarks">
              <a-textarea v-model="model.designRemarks" rows="4" placeholder="请输入设计备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设计负责人签字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="designerSignature">
              <j-select-user-by-dep v-model="model.designerSignature" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="节点进度跟进" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="progressTracking">
              <a-textarea v-model="model.progressTracking" rows="4" placeholder="请输入节点进度跟进" />
            </a-form-model-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="width: 100%;text-align: center;">
            <a-button icon="check" style="width: 126px" type="primary" @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: 'YjOrderDesignForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/erp/yjOrderDesign/add",
          edit: "/erp/yjOrderDesign/edit",
          queryById: "/erp/yjOrderDesign/queryById",
          queryOrderById: "/erp/yjOrder/queryById",
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
           getAction(this.url.queryOrderById,params).then((res)=>{
            if(res.success){
              console.log(res.result);
              this.edit (res.result);
            }
          });

        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>